﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Krile.Kernel.Data;

namespace Twinkle.Searchs
{
    public class SearchTwitterListedStatus : SingleSearchData
    {
        public override string Identification
        {
            get { return "Twitter_Twinkle_SearchData_List"; }
        }

        public string ParentAccountId = String.Empty;

        public string ListId = String.Empty;

        protected override bool IsMatchedInner(Status status)
        {

            //ステータスやアカウント情報のリファレンスが無ければすぐ戻る
            if (status == null || Kernel.Config.Accounts == null)
                return false;
            
            //結果格納用
            Tweak.TList list = null;

            foreach (var a in Kernel.Config.Accounts)
            {
                if (a.FollowingLists == null) continue;
                foreach (var l in a.FollowingLists)
                {
                    //すべてのリストを列挙
                    if (l.ParentUserId == ParentAccountId && l.Id == ListId)
                    {
                        list = l;
                        break;
                    }
                }
                if (list != null)
                    break;
            }
            if (list == null || list.Members == null)
                return false;

            System.Diagnostics.Debug.WriteLine(String.Join(Environment.NewLine, list.Members));
            return list.Members.Contains(status.User.Id);
        }

        public override string Description
        {
            get { return String.Format(Lang.General.TwitterListSearchDescription, "@" + ParentAccountId + "/" + ListId); }
        }

        public override string Name
        {
            get { return Lang.General.TwitterListSearchName; }
        }

        public override object Clone()
        {
            return new SearchTwitterListedStatus() { ListId = ListId, Exclude = Exclude };
        }

        public override void ReadXml(System.Xml.XmlReader reader)
        {
            if (reader.Name != "ParentAccountId")
                reader.ReadToDescendant("ParentAccountId");
            ParentAccountId = reader.ReadElementString("ParentAccountId");
            ListId = reader.ReadElementString("ListId");
        }

        public override void WriteXml(System.Xml.XmlWriter writer)
        {
            writer.WriteElementString("ParentAccountId", ParentAccountId);
            writer.WriteElementString("ListId", ListId);
        }

        public override Krile.Kernel.Control.SDataArgSetUI GetArgSetUI()
        {
            return new Controls.ListArgSet(this);
        }

        public override string Method
        {
            get { return "ListedIn"; }
        }

        public override string Class
        {
            get { return "Twitter"; }
        }

        public override void AcceptArgument(object[] arguments)
        {
            AssertTypeof(arguments, typeof(string), typeof(string));
            this.ParentAccountId = arguments[0] as string;
            this.ListId = arguments[1] as string;
        }

        protected override string WriteArguments()
        {
            return "\"" + this.ParentAccountId + "\", \"" + this.ListId + "\"";
        }
    }
}
